using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CodeStory.Core.UI;
using CodeStory.Core;
using CodeStory.News.ObjectEntity;

namespace CodeStory.WebApplication.Modules.News.Admin
{
	[NavigationPortlet("CodeStory_News_Admin_CreateCategory")]
	public partial class CreateCategory : Portlet
	{
		protected override void OnInit(EventArgs e) {
			Load += new EventHandler(CreateCategory_Load);
			btnCreate.Click += new EventHandler(ButtonCreate_Click);
			base.OnInit(e);
		}

		void ButtonCreate_Click(object sender, EventArgs e) {
			try {
				string categoryid = Request.QueryString["categoryid"];
				NewsCategoryObject obj;
				if (string.IsNullOrEmpty(categoryid)) {
					obj = NewsCategoryObject.New();
					obj.CategoryID = Guid.NewGuid();
				} else {
					obj = NewsCategoryObject.Read(new Guid(categoryid));
				}
				obj.CategoryName = txtCategoryName.Text;
				obj.Save();
			} catch {
			}
		}

		void CreateCategory_Load(object sender, EventArgs e) {
			if (!IsPostBack) {
				string categoryid = Request.QueryString["categoryid"];
				if (!string.IsNullOrEmpty(categoryid)) {
					BindCategory();
				}
			}
		}

		private void BindCategory() {
			try {
				string categoryid = Request.QueryString["categoryid"];
				NewsCategoryObject obj = NewsCategoryObject.Read(new Guid(categoryid));
				if (obj != null) {
					txtCategoryName.Text = obj.CategoryName;
				}
			} catch {
			}
		}
	}
}